package com.platform.car.service.impl;

import com.platform.car.dao.*;
import com.platform.car.model.*;
import com.platform.car.service.ParkingpayService;
import com.platform.common.base.impl.BaseServiceImpl;
import com.platform.common.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Transactional
@Service
public class ParkingpayServiceImpl extends BaseServiceImpl<Parkingpay> implements ParkingpayService {

    @Autowired
    private ParkingpayDao parkingpayDao;
    @Autowired
    private ParkuseDao parkuseDao;
    @Autowired
    private ParkinglotDao parkinglotDao;
    @Autowired
    private ParkingDao parkingDao;

    @Autowired
    private LockDao lockDao;

    @Override
    public Page<Parkingpay> findByPage(Page<Parkingpay> page, Parkingpay parkingpay) {
        List<Parkingpay> parkingpayList=parkingpayDao.findByPage(page, parkingpay);//查询获取数据库中的数据
        if(parkingpayList.size()>0){
            for (Parkingpay p:parkingpayList) {//遍历
                if(p.getParkuseId() != null && !"".equals(p.getParkuseId())){
                    System.out.println("------"+p.getParkuseId()+"-------");
                    Parkuse parkuse = parkuseDao.get(p.getParkuseId());//车位信息
                    if(parkuse!=null){
                        if(parkuse.getParkinglotId()!=null && !"".equals(parkuse.getParkinglotId())){
                            Parkinglot parkinglot = parkinglotDao.selectById(parkuse.getParkinglotId());
                            p.setParkinglot(parkinglot);//车位
                            System.out.println(parkinglot.getNumber());
                            if(parkinglot!=null && parkinglot.getParkingId() !=null &&!"".equals(parkinglot.getParkingId())){
                                p.setParking(parkingDao.get(parkinglot.getParkingId()));//停车场
                                System.out.println(parkingDao.get(parkinglot.getParkingId()).getName());
                            }

                        }
                    }

                }
            }
        }
        page.setResults(parkingpayList);
        return page;
    }


    @Override
    public double[] computeMoney(Parkuse parkuse, Parking parking) {
        Date goTime = parkuse.getUseTime();//进来时间
        Date outTime = parkuse.getLeaveTime();//出去时间
        long goTimeStr = goTime.getTime();//进场
        long outTimeStr = outTime.getTime();//出
        long diff = outTimeStr - goTimeStr; //两个事件相差的毫秒数
        long hour = (diff / (60 * 60 * 1000));//小时数
        long min = ((diff / (60 * 1000)) - hour * 60);//分数
        long sec = (diff / 1000 - hour * 60 * 60 - min * 60);//秒数
        if (min > 0 || sec > 0) {
            hour = hour + 1;
        }
        Double day = Math.floor(hour / 24);//天数
        long yu = hour % 24;//余下的小时数
        int light = 0;//白天时长
        int night = 0;//晚上时长
        if (day > 0) {
            light= (int)(day * 12);
            night= (int)(day * 12);
        }
        if (yu > 0) {
            Calendar rightNow = Calendar.getInstance(); // 一个静态方法，返回的是子类对象
            rightNow.setTime(goTime); //进场时间
            int h = rightNow.get(Calendar.HOUR_OF_DAY);
            //获取停车时间的小时是几点
            long hyu = yu + h;     //停车时间的小时数  加上余下的小时数
            if (h >= 6 && hyu <= 24) { //6点及6点后停车，24点之前取车
                light = light + Integer.valueOf(String.valueOf(yu));
            } else if (h < 6 && hyu <= 24 && hyu > 6) { //6点前停车，6点之后，24点之前取车
                night = night + Integer.valueOf(String.valueOf(6 - h));
                light = light + Integer.valueOf(String.valueOf(hyu - 6));
            } else if (h < 6 && hyu <= 6) {//6点前停车，6点前取车
                night = night + Integer.valueOf(String.valueOf(yu));
            } else if (h >= 6 && hyu > 24) {//6点及6点后停车，24点之后取车
                if (hyu - 24 >= 6) {
                    night = night + 6;
                    light = light + Integer.valueOf(String.valueOf(yu - 6));
                } else {
                    night = night + Integer.valueOf(String.valueOf(hyu - 24));
                    light = light + Integer.valueOf(String.valueOf(24 - h));
                }
            } else if (h < 6 && hyu > 24) {//6点前停车，24点之后取车
                night = night + Integer.valueOf(String.valueOf(yu - 18));
                light = light + Integer.valueOf(String.valueOf(42 - hyu));
            }
        }
        double bai=Double.valueOf(light)*parking.getDayFee();//白天费用
        double wan=Double.valueOf(night)*parking.getNightFee();//晚上费用
        double totalMoney=bai+wan;//总费用
        double[] expense=new double[]{Double.valueOf(light),Double.valueOf(night),totalMoney};
        return  expense;
    }

    @Override
    public Parkingpay selectParkuserId(Integer parkuseId) {
        return parkingpayDao.selectParkuserId(parkuseId);
    }

    @Override
    public List<Parkingpay> selectByParkuserId(Integer  parkuseId) {
        return parkingpayDao.selectByParkuserId(parkuseId);
    }

    @Override
    public void payMoney(Parkuse parkuse) {
        parkuse.setLeaveTime(new Date());
        parkuseDao.update(parkuse);//修改时间，方便支付计算
        Integer plId =parkuse.getParkinglotId();//停车位的ID
        Parkinglot parkinglot = parkinglotDao.selectById(plId);
        Integer parkingId = parkinglot.getParkingId();
        Parking parking=parkingDao.get(parkingId);
        if(parkinglot.getSwitchLock() !=null && "1".equals(parkinglot.getSwitchLock())){//有地锁的情况
            Integer lockId=parkinglot.getLockId();//获取到地锁的ID
            Lock lock = lockDao.get(lockId);
            lock.setStatus("0");//地锁的状态为空闲状态
            lockDao.update(lock);//修改地锁的状态
        }
        parkinglot.setSwitchStatus("1");//未预约状态
        parkinglotDao.updateById(parkinglot);
        double[] expense = computeMoney(parkuse,parking);
        Parkingpay parkingpay=new Parkingpay(parkuse.getId(),Integer.valueOf((int)expense[0]),Integer.valueOf((int)expense[1]),expense[2],"0");
        parkingpayDao.saveData(parkingpay);
        parkuse.setStatus("3");
        parkuseDao.update(parkuse);
    }

    @Override
    public Parkingpay selectById(Integer parkingpayId) {
        return parkingpayDao.selectById(parkingpayId);
    }
}
